package cn.didi.feidi.androidorder.fragment.personal;


import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.api.Api;
import cn.didi.feidi.androidorder.api.ApiResponse;
import cn.didi.feidi.androidorder.api.ErrorEvent;
import cn.didi.feidi.androidorder.assembly.CommomDialog;
import cn.didi.feidi.androidorder.core.ActionCallbackListener;
import cn.didi.feidi.androidorder.core.AppActionRequest;
import cn.didi.feidi.androidorder.fragment.FeidiBaseFragment;
import cn.didi.feidi.androidorder.manager.UserManager;
import cn.didi.feidi.androidorder.model.User;


public class PersonChangeNameFragment extends FeidiBaseFragment {

    private TextView saveChangeName;
    private EditText editChangeName;

    private static final String TAG = "PersonChangeNameFrag";

    /**
     * 与activity关联，后面可考虑单例模式
     *
     * @param bundle
     * @return
     */
    public static PersonChangeNameFragment newInstance(Bundle bundle) {
        PersonChangeNameFragment fragment = new PersonChangeNameFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View initFragment(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_person_change_name, container, false);
        Log.d(TAG, "initFragment");
        return rootView;
    }

    @Override
    protected void initViews(View rootView) {
        editChangeName = (EditText) rootView.findViewById(R.id.person_change_person_name_edit);
        saveChangeName = (TextView) rootView.findViewById(R.id.save_person_change_name_btn);
    }

    @Override
    public void onResume() {
        super.onResume();
        editChangeName.setHint(UserManager.getName());
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void addListener() {
        saveChangeName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String name = editChangeName.getText().toString();
                if (name.length() == 0) {
                    Toast.makeText(getContext(), "输入不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                Map<String, String> data = new HashMap<>();
                data.put("userId", UserManager.getUserId());
                data.put("token", UserManager.getToken());
                data.put("name", name);
                executeRequest(new AppActionRequest<String>(Api.UPDATE_USER_PERSON_INFO, data, new TypeToken<ApiResponse<String>>() {
                }.getType(), new ActionCallbackListener<String>() {
                    @Override
                    public void onSuccess(String message, String data) {
                        // 获取到 editFeidiNumber的值并上传  设置飞滴号已设置的标志
                        Toast.makeText(getContext(), "修改成功", Toast.LENGTH_SHORT).show();
                        UserManager.setName(name);
                        // 修改成功 弹出对话框
                        //弹出提示框
                        CommomDialog dialog = new CommomDialog(getContext(), R.style.dialog, "修改成功", new CommomDialog.OnCloseListener() {
                            @Override
                            public void onClick(Dialog dialog, boolean confirm) {
                                if (confirm) {
                                    dialog.dismiss();
                                    mActivity.finish();
                                }
                            }
                        }, true, true).setPositiveButton("好", "#212121");
                        dialog.show();
                    }

                    @Override
                    public void onFailure(int errorEvent, String message) {
                        Log.d(TAG, "errorEvent:" + errorEvent + "message=" + message);
                        switch (errorEvent) {
                            default:
                                makeToast("捕捉到未知错误");
                                break;
                        }
                    }
                }));

            }
        });

    }

}
